<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .login-box {
            width: 400px;
            height: 250px;
            border: 1px solid black;
            text-align: center;
        }

        input {
            height: 40px;
            padding-left: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
            width: 200px;
            outline: none;
            box-sizing: border-box;
            margin-top: 10px;
        }
    </style>
</head>

<body>
    <div class="login-box">
        <div class="zhuce">注册新账号</div>
        <div class="block">
            <input type="text" placeholder="请输入账号">
        </div>
        <div class="block">
            <input type="password" placeholder="请输入密码">
        </div>
        <div class="block">
            <input type="password" placeholder="确认密码">
        </div>
        <div class="block">
            <input type="button" value="注册">
        </div>
    </div>
</body>
<script src="./ajax.js"></script>
<script>
    //获取四个input输入框
    var inputs = document.querySelectorAll('input')
    //给button绑定点击事件
    inputs[3].onclick = function () {
        //给表单做校验
        if (inputs[0].value && inputs[1].value && inputs[2].value) {
            //（判断两个密码是否一致)
            if (inputs[1].value == inputs[2].value) {
                if (/^\d{4}$/.test(inputs[1].value)) {
                    // 通过校验，发起注册请求
                    ajax('get', 'https://yantianfeng.com/api/register', {
                        username: inputs[0].value,
                        password: inputs[1].value,
                        // password:inputs[2].value
                    }, function (data) {
                        alert('注册成功')
                        // console.log(data);
                    })
                } else {
                    alert('请输入正确的用户名')
                }
            } else {
                alert('两次密码不一致')
            }
        } else {
            alert('用户名或密码不能为空')
        }
    }
</script>

</html>